#ifndef SINGLETON_H
#define SINGLETON_H

#include <global.h>

//单例模式
template<typename T>
class Singleton{
protected:
    Singleton() = default;
    Singleton(const Singleton<T>&) = delete;
    Singleton& operator=(const Singleton<T>&) = delete;
    static std::shared_ptr<T> _instance;
public:
/******************************************************************************
 *
 * @file       singleton.h
 * @brief      推荐使用这种方式来初始化单例模式，更安全
 *
 * @author     wangzn2016
 * @date       2024/11/18
 * @history
 *****************************************************************************/
    static std::shared_ptr<T> GetInstance() {
        static std::once_flag s_flag;
        std::call_once(s_flag, [&]() {
            _instance = std::shared_ptr<T>(new T);
        });
        return _instance;
    }
    void PrintAddress() {
        std::cout << _instance.get() << std::endl;
    }
    ~Singleton()
    {
        std::cout << "this is singleton destruct" << std::endl;
    }
};
//模板类的static成员需要放在声明文件中
template<typename T>
std::shared_ptr<T> Singleton<T>::_instance = nullptr;

#endif // SINGLETON_H
